﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Xml;

namespace RNT.Common.Utils
{
    public sealed class AppConfig
    {
        private static AppConfig instance = null;
        private static object _lock = new object();
        private AppConfig() { }

        public static AppConfig Instance
        {
            get
            {
                if (instance == null)
                {
                    lock (_lock)
                    {
                        if (instance == null)
                        {
                            instance = new AppConfig();
                        }
                    }
                }

                return instance;
            }
        }

        /// <summary>
        /// 获取应用程序文件名
        /// </summary>
        /// <returns></returns>
        public string GetAppExeName()
        {
            string exepath = Application.ExecutablePath;
            return exepath.Substring(Application.ExecutablePath.LastIndexOf("\\") + 1);
        }

        /// <summary>
        /// 返回*.exe.config文件中appSettings配置节点的value值
        /// </summary>
        /// <param name="key">键</param>
        /// <returns>返回节点的value值，不存在键时返回 null</returns>
        public string GetConfig(string key)
        {
            //foreach (string k in ConfigurationManager.AppSettings)
            //{
            //    if (k == key)
            //    {
            //        return ConfigurationManager.AppSettings[key];
            //    }
            //}

            XmlDocument document = new XmlDocument();

            string configPath = AppDomain.CurrentDomain.BaseDirectory + GetAppExeName().ToLower() + ".config";
            document.Load(configPath);

            XmlNodeList nodes = document.GetElementsByTagName("add");
            for (int i = 0; i < nodes.Count; i++)
            {
                XmlAttribute attr = nodes[i].Attributes["key"];

                if (attr != null && attr.Value == key)
                {
                    attr = nodes[i].Attributes["value"];
                    return attr.Value;
                    //break;
                }
            }

            return null;
        }

        /// <summary>
        /// 设置*.exe.config文件中appSettings配置节点key的value值
        /// </summary>
        /// <param name="key">键</param>
        /// <param name="value">值</param>
        /// <returns>返回修改状态，失败或不存在时返回 false</returns>
        public bool SetConfig(string key, string value)
        {
            bool isModify = false;
            XmlDocument document = new XmlDocument();

            string configPath = AppDomain.CurrentDomain.BaseDirectory + GetAppExeName().ToLower() + ".config";
            document.Load(configPath);

            XmlNodeList nodes = document.GetElementsByTagName("add");
            for (int i = 0; i < nodes.Count; i++)
            {
                XmlAttribute attr = nodes[i].Attributes["key"];

                if (attr != null && attr.Value == key)
                {
                    attr = nodes[i].Attributes["value"];
                    attr.Value = value;
                    isModify = true;
                    break;
                }
            }

            document.Save(configPath);
            return isModify;
        }
    }
}
